﻿using System;
using System.Collections.Generic;
using System.Text;
using System.EnterpriseServices.Internal;
using Wagner.Utilities;

namespace Wagner.SharePoint.Utilities
{
	internal class Gac : OperationBase
	{
		internal Gac()
		{ }

		internal void Run()
		{
			string file = ValidateParameter("file", true, null);
			string remove = ValidateParameter("remove", false, null);
			string interop = ValidateParameter("interop", false, null);
			
			GacAssembly(file, remove, interop);
		}

		private void GacAssembly(string file, string remove, string interop)
		{
			try
			{
				Publish p = new Publish();

				if (Args["remove"] == "true")
				{
					Console.WriteLine(string.Format("Unregistering assembly '{0}' from the Global Assembly Cache.", file));
					p.GacRemove(file);
					
					if (Args["interop"] == "true")
					{
						// unregister assembly for COM interop
						p.UnRegisterAssembly(file);
					}
				}
				else
				{
					Console.WriteLine(string.Format("Registering assembly '{0}' to the Global Assembly Cache.", file));
					p.GacInstall(file);

					if (Args["interop"] == "true")
					{
						// registering assembly for COM interop
						p.RegisterAssembly(file);
					}
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}

		}

		internal override string HelpSummary
		{
			get
			{
				return @"
-o Gac 
     Registers or unregisters an assembly in the Global Assembly Cache. No
     need to use (or hunt down) the GacUtil.exe. Faster.";
			}
		}

		internal override string HelpDetail
		{
			get
			{
				return HelpSummary + @"
Usage:
  SPDev.exe -o Gac -file ""<assembly name>"" [-remove] [-interop]
    
    Parameters: 
     -file ""<assembly name>""
       Name (or name and path) of the assembly.
 
     -remove
       Removes (unregisters) the assembly from the Global Assmbly Cache.
 
     -interop
       Registers the assembly for COM interop.
 
Examples:
 SPDev.exe -o Gac -file ""MyAssembly.dll"" 
 SPDev.exe -o Gac -file ""MyAssembly.dll"" -remove
 SPDev.exe -o Gac -file ""MyAssembly.dll"" -interop";
			}
		}


	}
}
